<?php
namespace Home\Controller;
use Common\Controller\BaseController;
class IndexController extends BaseController {
    /**
    **首页
    **/
    public function index(){
        $this->check_login();

//        S('number_'.$_SESSION['userid'],null);
//        die;

        //判断是否第一次
//        $data['is_first'] = S('first_'.$_SESSION['userid']);
        $userid = $_SESSION['userid'];

        $isWinGift=I("isWinGift");

        //获取当前可用次数
        $data['rest'] = $this->getPlayNumber();

        //获取奖品
        $prize_list = S('prize_list');
        if(!$prize_list){
            $prize_list = M('chance') -> field('pk_id,prize_name,total_number,number,worth') -> select();
            S('prize_list',$prize_list);
        }
        $data['prize_list'] = $prize_list;

        //我的奖品
        $prize_list = S('my_prize_list_'.$userid);
        if(!$prize_list){
            $prize_list = M('myprize') ->  where('fk_user_id=%d',$_SESSION['userid'])->field('pk_id,worth,shop,prize_name,LEFT(create_time,10) AS create_time') -> select();
            S('my_prize_list_'.$userid , $prize_list);
        }
        $data['my_prize_list'] = $prize_list;

        $this->assign("isWinGift",$isWinGift);
        $this -> showView($data,'./Public/Index.html');
    }


    /**
    **用户登录
    **/
    public function login($success_url){
    	!$_GET['code'] && $this ->rtnResult(77,'非法操作');
    	$uInfo = $this -> oauth2_user_info($_GET['code']);
    	$_SESSION['uInfo'] = $uInfo;

    	//用户是否已经存在
    	$User = M('User');
    	$info = $User -> where("openid='%s'",$uInfo['openid']) -> field('pk_id,nickname,headimgurl') -> find();
    	if(empty($info)){
    		//加入到数据库
    		$data['openid'] = $uInfo['openid'];
    		$data['nickname'] = $uInfo['nickname'];
    		$data['headimgurl'] = $uInfo['headimgurl'];
    		$data['sex'] = $uInfo['sex'];
    		$data['country'] = $uInfo['country'];
    		$data['province'] = $uInfo['province'];
    		$data['city'] = $uInfo['city'];
    		$uid = $User ->add($data);
    		!$uid && $this ->rtnResult(1,'login error');

            //设置可用次数（默认3次）
            S('number_'.$uid , C('FREE_NUM'));
    	}
    	$_SESSION['headimgurl'] = $info['headimgurl'] ? $info['headimgurl'] : $uInfo['headimgurl'];
    	$_SESSION['nickname'] = $info['nickname'] ? $info['nickname'] : $uInfo['nickname'];
    	$_SESSION['openid'] = $info['openid'] ? $info['openid'] : $uInfo['openid'];
    	$_SESSION['userid'] = $info['pk_id'] ? $info['pk_id'] : $uid;
    	if($success_url){
    		header('location:'.U('Home/Index/index'));
    	}
    }



    //游戏说明
    public function activityDesc(){
    	$this -> showView('','./Public/game.html');
    }
    public function gift(){
        $this->check_login();

        $data['rest'] = $this->getPlayNumber();
        $data['freerest'] = $data['rest'] -1;

        $sourcePage = I('get.sourcePage');
        if($sourcePage == 'advert'){
            //获取抽奖结果
            $result = R('Member/Member/draw');
            $data['info'] = $result;

            //可参与次数减1
            $this->decNumber($data['rest']);

//            dump($data['info']);
        }
        $this -> showView($data,'./Public/gift.html');
    }
    public function advert(){
        $this->check_login();
        $this -> showView('','./Public/advert.html');
    }
    //游戏开始
    public function play(){
        $this->check_login();
        $this -> showView('','./Public/play.html');
    }
}